package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.BlogMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Blog;
import cn.tedu.weibo.pojo.entity.User;
import cn.tedu.weibo.pojo.vo.BlogIndexVO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.result.JsonResult;
import cn.tedu.weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
@Api(tags = "博客模块")
@Validated
public class BlogController {
    @Autowired
    private BlogMapper blogMapper;
    @PostMapping("/insert")
    @ApiOperation("发布微博")
    public JsonResult addBlog(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        System.out.println(weiboDTO);
        User user = (User) session.getAttribute("user");
        System.out.println("当前登录用户信息是"+user);
        if (user==null){//未登录
            return new JsonResult(Status.NOT_LOGIN);
        }
        Blog blog = new Blog();
        blog.setContent(weiboDTO.getContent());
        blog.setCreated(new Date());
        blog.setUserId(user.getId());
        blogMapper.insert(blog);
        return JsonResult.success();
    }
    @GetMapping("/selectIndex")
    @ApiOperation("首页微博列表")
    public JsonResult findIndexBlog(){
        return JsonResult.success(blogMapper.findAll());
    }
    @GetMapping("/selectById")
    @ApiOperation("查看微博详情")
    @ApiImplicitParam(name="id",value = "微博id",required = true,example = "1",dataType = "int")
    public JsonResult findById(@Range(min = 1,max = 9999,message = "id只能在1-9999之间")@RequestParam Integer id){
//        if (id<1){
//            throw new IllegalArgumentException("id不能小于1");
//        }
        return JsonResult.success(blogMapper.findById(id));
    }
    @GetMapping("/findByIdAndNickname")
    @ApiOperation("根据用户及id查看微博详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "微博id", required = true, example = "1", dataType = "int"),
            @ApiImplicitParam(name = "nickname", value = "作者昵称", required = true, example = "阿三", dataType = "String")
    })
    public JsonResult findByIdAndNickname(Integer id, String nickname){
        return JsonResult.success();
    }
}
